package com.smartthinking.dive;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;



public class FavoritosArrayAdapter extends BaseAdapter implements SectionIndexer{
	//public class FavoritosArrayAdapter extends BaseAdapter{

	private final Context context;
	private final ArrayList<Dictionary<String,String>> values;
	Utilities util;
	private static final String HTTPS = "https://";
	private static final String HTTP = "http://";
	
	//	private static String sections = "abcdefghilmnopqrstuvz";

	HashMap<String, Integer> alphaIndexer;
	String[] sections;

	//	public FavoritosArrayAdapter(Context context, int textViewResourceId,
	//			List<String> objects) {
	//		super(context, textViewResourceId, objects);
	//	}


	public FavoritosArrayAdapter(Context context, ArrayList<Dictionary<String,String>> values) {
		this.context = context;
		this.values = values;
		util = new Utilities();

		/******/
		alphaIndexer = new HashMap<String, Integer>();
		int size = this.values.size();
		Log.i("size", ""+size);
		for (int x = 0; x < size; x++) {
			if(this.values.get(x).get("Header") == null){
				if(this.values.get(x).get("eve_nombre") != null){
				String s = this.values.get(x).get("eve_nombre").toString();
				Log.i("eve_nombre_adapter", ""+s);
				// get the first letter of the store
				String ch = s.substring(0, 1);
				// convert to uppercase otherwise lowercase a -z will be sorted
				// after upper A-Z
				ch = ch.toUpperCase();
				// put only if the key does not exist
				if (!alphaIndexer.containsKey(ch))
					alphaIndexer.put(ch, x);
			}
			}
		}

		Set<String> sectionLetters = alphaIndexer.keySet();
		// create a list from the set to sort
		ArrayList<String> sectionList = new ArrayList<String>(
				sectionLetters);
		Collections.sort(sectionList);
		sections = new String[sectionList.size()];
		sections = sectionList.toArray(sections);



		/******/


	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final Dictionary<String,String> dic = values.get(position);

		//    View convertView1;

		if(dic.get("type").equals("1")){
			convertView = inflater.inflate(R.layout.header, parent, false);
			TextView txtfecha = (TextView) convertView.findViewById(R.id.textView_HEADER);
			txtfecha.setText(dic.get("head").toString());
			RelativeLayout relative = (RelativeLayout) convertView.findViewById(R.id.relative_HEADER);
			relative.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
				}
			});

		}else{

			if(dic.get("Header") != null){
				convertView = inflater.inflate(R.layout.banner_evento, parent, false);
				ImageView banner = (ImageView) convertView.findViewById(R.id.imageViewbannereve);
				File storagePath = Environment.getExternalStorageDirectory();

				//			Bitmap bmp = BitmapFactory.decodeFile(storagePath+"/Squenda/"+dic.get("Header").toString());
				Bitmap bmp = BitmapFactory.decodeFile(context.getFilesDir()+"/localdata/"+dic.get("Header").toString());

				banner.setImageBitmap(bmp);
				banner.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						String tag2="";
						Log.i("BANNER DESTINO", ""+dic.get("Destino"));
						Uri uri = Uri.parse(dic.get("Destino"));
						if(!dic.get("Destino").startsWith(HTTP) && !dic.get("Destino").startsWith(HTTPS)){
							tag2 =  HTTP + dic.get("Destino");
						}
						Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tag2));
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent);
					}
				});

			}else{
				String nombre ="";
				String nombre0 ="";
				convertView = inflater.inflate(R.layout.lista_dest_eventos, parent, false);
				TextView texto = (TextView) convertView.findViewById(R.id.textViewdesteve);
				TextView texto0 = (TextView) convertView.findViewById(R.id.textViewdesteve0);
				RelativeLayout relative = (RelativeLayout) convertView.findViewById(R.id.RelativeLayoutdesteve);
				Log.i("dic", "dic ="+dic.toString());
				nombre = dic.get("eve_nombre").toString();
				nombre0 = values.get(position).get("proveedores");
				Log.i("eve_proveedores1", "eve_proveedores1 ="+values.get(position).get("proveedores"));
				texto.setText(nombre);
				texto0.setText(nombre0);


				if(dic.get("eve_destacado").equals("1")){
					texto.setTextColor(Color.WHITE);
					relative.setBackgroundColor(Color.parseColor("#B89EEC"));
				}else{
					texto.setTextColor(Color.BLACK);
					relative.setBackgroundColor(Color.WHITE);
				}
			}  
		}



		return convertView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return values.size();
	}

	@Override
	public Object getItem(int arg0) {

		return values.get(arg0).get("eve_nombre");
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}



	@Override
	public int getPositionForSection(int section) {

		return alphaIndexer.get(sections[section]);

		//		return 0;
	}

	@Override
	public int getSectionForPosition(int position) {

		return 0;
	}

	@Override
	public Object[] getSections() {
		return sections;

		//		return null;
	}



	//	private class MyCustomAdapter extends BaseAdapter {
	//
	//		  private static final int TYPE_ITEM = 0;
	//		  private static final int TYPE_SEPARATOR = 1;
	//		  private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
	//
	//		  //  private ArrayList mData = new ArrayList();
	//		  private LayoutInflater mInflater;
	//
	//		  //        private TreeSet mSeparatorsSet = new TreeSet();
	//		  private ArrayList<Dictionary<String, String>> arrayData;
	//
	//
	//		  public MyCustomAdapter(ArrayList<Dictionary<String, String>> arrayData) {
	//		   mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	//		   this.arrayData = arrayData;
	//
	//		  }
	//
	//		  @Override
	//		  public int getCount() {
	//		   return arrayData.size();
	//		  }
	//
	//		  @Override
	//		  public String getItem(int position) {
	//		   return ""+arrayData.get(position);
	//		  }
	//
	//		  @Override
	//		  public long getItemId(int position) {
	//		   return position;
	//		  }
	//
	//		  @Override
	//		  public View getView(int position, View convertView, ViewGroup parent) {
	//		   ViewHolder holder = null;
	//		   //            int type = getItemViewType(position);
	//		   //            System.out.println("getView " + position + " " + convertView + " type = " + type);
	//		   if (convertView == null) {
	//		    holder = new ViewHolder();
	//		    if(position == 0){
	//		     convertView = mInflater.inflate(R.layout.headernavigationdrawer, null);
	//		     holder.textName = (TextView)convertView.findViewById(R.id.txt_tutor);
	//		     holder.textRol = (TextView)convertView.findViewById(R.id.txt_rol);
	//		     holder.imageRol = (ImageView)convertView.findViewById(R.id.image_rol);
	//		     holder.layoutParent = (LinearLayout)convertView.findViewById(R.id.layout_parent);
	//
	//		    }else{
	//		     convertView = mInflater.inflate(R.layout.itemnavigationdrawer, null);
	//		     holder.textRow = (TextView)convertView.findViewById(R.id.txt_item);
	//		     holder.numUsers = (TextView)convertView.findViewById(R.id.txt_numero_usuarios);
	//		     holder.imageRow = (ImageView)convertView.findViewById(R.id.image_item);
	//
	//		    }
	//		    convertView.setTag(holder);
	//		   } else {
	//		    holder = (ViewHolder)convertView.getTag();
	//		   }
	//
	//		   if(position == 0){
	//		    holder.textName.setText(arrayData.get(position).get("nombretutor"));
	//		    holder.textRol.setText(arrayData.get(position).get("rol"));
	//		    holder.imageRol.setImageResource(Integer.parseInt(arrayData.get(position).get("imagen")));
	//		    holder.layoutParent.setBackgroundColor(Color.parseColor("#70BE55"));
	//
	//		   }else{
	//		    if(position == 1){
	//		     holder.textRow.setText(arrayData.get(position).get("elementomenu"));
	//		     holder.numUsers.setVisibility(View.VISIBLE);
	//		     holder.numUsers.setText(arrayData.get(position).get("usuariosautorizados"));
	//		     holder.imageRow.setImageResource(Integer.parseInt(arrayData.get(position).get("icono")));
	//		    }else{
	//		     holder.textRow.setText(arrayData.get(position).get("elementomenu"));
	//		     holder.numUsers.setVisibility(View.GONE);
	//		     holder.imageRow.setImageResource(Integer.parseInt(arrayData.get(position).get("icono")));
	//		    }
	//		   }
	//
	//		   return convertView;
	//		  }
	//
	//		 }
}

